﻿(function () {
    var appComCalc = angular.module('commonCalc', []);

    appComCalc.controller('commonCalculator', function ($scope, $filter, Services) {

        $scope.calculations = {
            HDNOR: 0,
            ETBAnch: 0,
            HDATB: 0,
            CaseFour: 0,
            LessShifting: 0,
            TotalTime: 0,
            LayTimeAllowed: 0,
            TimeDemmurage: 0,
            TotalTimeInUsed: 0,
            Word: undefined,
            Case: undefined

        };

        $scope.getDateTimeString = function (date) {
            return $filter('date')(date, 'dd.MM.yyyy HH:mm');
        };

        $scope.substractDates = function (dt1, dt2) {
            var result = parseFloat(((dt1.getTime() - dt2.getTime()) / (1000 * 3600)).toFixed(2));
            var zoneDiff = (dt1.getTimezoneOffset() - dt2.getTimezoneOffset())/60;
            return result - zoneDiff;
        };

        $scope.calcCase1 = function (port, index) {
            var hd = port.ETD_ATDTime;
            var nor = port.ETA_NORTime;
            var etb = port.ETB_ATBTime;
            var aa = port.AATime;
            if (hd != null && nor != null && etb != null && aa != null) {
                $scope.condition = 1;
                $scope.calculations.HDNOR = $scope.substractDates(hd, nor) - 6; //lol error no 6
                $scope.calculations.ETBAnch = $scope.substractDates(etb, aa);
                $scope.calculations.TotalTime = $scope.calculations.HDNOR - $scope.calculations.ETBAnch;
                $scope.flag = true;
            }
            else {
                $scope.condition = 0;
                $scope.calculations.TotalTime = 0;
                $scope.changeIndex(index);
            }
        };

        $scope.calcCase2 = function (port, index) {
            var hd = port.ETD_ATDTime;
            var atb = port.ETB_ATBTime;
            var nor = port.ETA_NORTime;
            if (hd != null && atb != null && nor != null) {
                $scope.condition = 2;
                $scope.calculations.HDATB = $scope.substractDates(hd, atb);
                $scope.calculations.TotalTime = $scope.calculations.HDATB;
                $scope.flag = true;
            }
            else {
                $scope.condition = 0;
                $scope.calculations.TotalTime = 0;
                $scope.changeIndex(index);
            }
        };

        $scope.calcCase4and5 = function (port, Case, cond, index) {
            var hd = port.ETD_ATDTime;
            var nor = port.ETA_NORTime;
            var aa = port.AATime;
            var flag = true;
            if (aa == null || port.AAMarker == '')
                flag = false;
            var cases = port.ETB_ATBTime;
            if (hd != null && nor != null && cases != null) {
                $scope.condition = cond;
                if (port.FinalCalculationOption == 1) 
                    $scope.calculations.CaseFour = $scope.substractDates(hd, nor);
                else 
                    $scope.calculations.CaseFour = $scope.substractDates(hd, cases);
                $scope.calculations.LessShifting = 0;
                if (flag)
                    $scope.calculations.LessShifting = $scope.substractDates(cases, aa);
                $scope.calculations.TotalTime = $scope.calculations.CaseFour - $scope.calculations.LessShifting;
                $scope.flag = true;
            }
            else {
                $scope.condition = 0;
                $scope.calculations.TotalTime = 0;
                $scope.changeIndex(index);
            }
        };

        $scope.changeIndex = function (index) {
            if ($scope.globalFlag) {
                $scope.index = index;
                $scope.globalFlag = false;
            }
        };

        $scope.calcCondition = function (port, i, isChecking) {
            if (port.AAMarker == null || port.AAMarker == undefined)
                port.AAMarker = '';
            switch (port.ETAMarker + port.AAMarker + port.ETBMarker + port.ETDMarker) {
                case 'NOR+6hAnchor aweighETBHD':
                    $scope.calcCase1(port, i);
                    break;
                case 'NOR+6hAnchor aweighETBETD':
                    $scope.calcCase1(port, i);
                    break;
                case 'NOR+6hAnchor aweighATBHD':
                    $scope.calcCase1(port, i);
                    break;
                case 'NOR+6hAnchor aweighATBETD':
                    $scope.calcCase1(port, i);
                    break;
                case 'NOR+6hATBHD':
                    $scope.calcCase2(port, i);
                    break;
                case 'NOR+6hATBETD':
                    $scope.calcCase2(port, i);
                    break;
                    //case 'NOR+6hETBETD':
                    //    $scope.condition = 3;
                    //    break;
                case 'NORATBHD':
                    $scope.calcCase4and5(port, 'ATB', 5, i);
                    break;
                case 'NORATBETD':
                    $scope.calcCase4and5(port, 'ATB', 5, i);
                    break;
                case 'NORETBHD':
                    $scope.calcCase4and5(port, 'ETB', 4, i);
                    break;
                case 'NORETBETD':
                    $scope.calcCase4and5(port, 'ETB', 4, i);
                    break;
                case 'NORAnchor aweighETBHD':
                    $scope.calcCase4and5(port, 'ETB', 4, i);
                    break;
                case 'NORAnchor aweighETBETD':
                    $scope.calcCase4and5(port, 'ETB', 4, i);
                    break;
                case 'NORAnchor aweighATBHD':
                    $scope.calcCase4and5(port, 'ATB', 5, i);
                    break;
                case 'NORAnchor aweighATBETD':
                    $scope.calcCase4and5(port, 'ATB', 5, i);
                    break;
                    //case 'ETAAnchor aweighETBHD':
                    //    $scope.condition = 6;
                    //    break;
                default:
                    $scope.condition = 0;
                    if (isChecking && (port.ETDMarker != 'HD' || port.ETD_ATDTime == null))
                        $scope.changeIndex(i);
                    return; break;
            }
        };

        $scope.init = function (type, id, isReal) {
            $scope.id = id;
            $scope.type = type;
            $scope.isReal = isReal;
        };

        $scope.$on('commonCalc', function () {
            if (!$scope.isReal || Services.common.oper == 'check')
                if ($scope.type == Services.common.type && $scope.id == Services.common.Id)
                    $scope.calculation(Services.common.ports, true);
        });

        $scope.calculation = function (ports, isCheck) {
            var isChecking = isCheck;
            if (isCheck == undefined)
                isChecking == false;
            $scope.flag = false;
            $scope.globalFlag = true;
            $scope.index = 0;
            var total = 0;
            for (var i = 0; i < ports.length; i++) {
                $scope.calculations.TotalTime = 0;
                $scope.calcCondition(ports[i], i, isChecking);
                total += $scope.calculations.TotalTime;
            }
            Services.changeSelected({ type: $scope.type, Id: $scope.id, index: $scope.index });
            Services.updateTotal({ type: $scope.type, Id: $scope.id, total: parseFloat(total.toFixed(2)), flag: $scope.flag });
        };
    });
})()